﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using EONFULLSIN2.Core;
using EONFULLSIN2.Models.Dto.Device;

namespace EONFULLSIN2.Services.Device.ZhuoLan
{
    /// <summary>
    /// 务必注册成单例
    /// </summary>
    public class ZhuoLanModbusService : ModbusBaseServices, IZhuoLanModbusService
    {
        private static byte slaveId = 1;
        private static ushort startAddress = 0x10;
        private static ushort numRegisters = 8;

        public override string IpAddress { protected set; get; } = "192.168.1.201";

        public override int Port { protected set; get; } = 8801;

        public async Task<string> GetDataAsync()
        {
            if (IsConnect)
            {
                string resData = string.Empty;
                var readData = await ReadInputRegistersAsync(slaveId, startAddress, numRegisters);

                if (readData != null && readData.Length > 0)
                {
                    resData = string.Join(" ", readData.Select(o => $"{o}:X4")); // 输出每个寄存器的十六进制值
                }
                return resData;
            }
            throw new Exception($"{IpAddress}:{Port},is not connect");
        }

    }
}
